import type { App } from "vue";
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";

export const Layout = () => import("@/layout/index.vue");

// 静态路由
export const constantRoutes: any[] = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },
  {
    path: "/401",
    component: () => import("@/views/error-page/401.vue"),
    meta: { hidden: true },
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: { hidden: true },
  },
];

export const roleRoutes = [
  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
    ],
  },
  {
    path: "/system",
    component: Layout,
    name: "/system",
    perm: "system",
    meta: {
      title: "系统管理",
      icon: "el-icon-setting",
      hidden: false,
      alwaysShow: false,
    },
    children: [
      {
        path: "device",
        component: () => import("@/views/system/device.vue"),
        name: "SystemDevice",
        perm: "system:device",
        meta: {
          title: "设备更新",
          hidden: false,
          alwaysShow: false,
        },
      },
      {
        path: "config",
        component: () => import("@/views/system/config.vue"),
        name: "SystemConfig",
        perm: "system:config",
        meta: {
          title: "系统配置",
          hidden: false,
          alwaysShow: false,
        },
      },
    ],
  },
  {
    path: "/operations",
    component: Layout,
    name: "/operations",
    perm: "operations",
    meta: {
      title: "运营管理",
      icon: "el-icon-setting",
      hidden: false,
      alwaysShow: false,
    },
    children: [
      {
        path: "agent",
        component: () => import("@/views/operations/agent.vue"),
        name: "OperationsAgent",
        perm: "operations:agent",
        meta: {
          title: "代理列表",
          hidden: false,
          alwaysShow: false,
        },
      },
      {
        path: "stock",
        component: () => import("@/views/operations/stock.vue"),
        name: "OperationsStock",
        perm: "operations:stock",
        meta: {
          title: "补货员列表",
          hidden: false,
          alwaysShow: false,
        },
      },
      {
        path: "guide",
        component: () => import("@/views/operations/guide.vue"),
        name: "OperationsGuide",
        perm: "operations:guide",
        meta: {
          title: "导购列表",
          hidden: false,
          alwaysShow: false,
        },
      },
    ],
  },
  {
    path: "/user",
    component: Layout,
    name: "/user",
    perm: "user",
    meta: {
      title: "用户管理",
      icon: "el-icon-User",
      hidden: false,
      alwaysShow: false,
    },
    children: [
      {
        path: "list",
        component: () => import("@/views/user/list.vue"),
        name: "userlist",
        perm: "user:list",
        meta: {
          title: "用户列表",
          hidden: false,
          alwaysShow: false,
        },
      },
      {
        path: "role",
        component: () => import("@/views/user/role.vue"),
        name: "userrole",
        perm: "user:role",
        meta: {
          title: "角色列表",
          hidden: false,
          alwaysShow: false,
        },
      },
    ],
  },
  {
    path: "/device",
    component: Layout,
    name: "/device",
    perm: "device",
    meta: {
      title: "设备管理",
      icon: "el-icon-Printer",
      hidden: false,
      alwaysShow: false,
    },
    children: [
      {
        path: "list",
        component: () => import("@/views/device/list.vue"),
        name: "DeviceList",
        perm: "device:list",
        meta: {
          title: "设备列表",
          hidden: false,
          alwaysShow: false,
        },
      },
      {
        path: "detail",
        component: () => import("@/views/device/detail.vue"),
        name: "DeviceDetail",
        perm: "device:detail",
        meta: {
          title: "设备详情",
          hidden: true,
          alwaysShow: false,
        },
      },
      {
        path: "template",
        component: () => import("@/views/device/template.vue"),
        name: "DeviceTemplate",
        perm: "device:template",
        meta: {
          title: "货道模板",
          hidden: false,
          alwaysShow: false,
        },
      },
      {
        path: "record",
        component: () => import("@/views/device/record.vue"),
        name: "DeviceRecord",
        perm: "device:record",
        meta: {
          title: "补货记录",
          hidden: false,
          alwaysShow: false,
        },
      },
      {
        path: "error",
        component: () => import("@/views/device/error.vue"),
        name: "DeviceError",
        perm: "device:error",
        meta: {
          title: "设备故障",
          hidden: false,
          alwaysShow: false,
        },
      }
    ],
  },
  {
    path: "/order",
    component: Layout,
    name: "/order",
    perm: "order",
    meta: {
      title: "订单管理",
      icon: "el-icon-coin",
      hidden: false,
      alwaysShow: false,
    },
    children: [
      {
        path: "list",
        component: () => import("@/views/order/list.vue"),
        name: "OrderList",
        perm: "order:list",
        meta: {
          title: "订单列表",
          hidden: false,
          alwaysShow: false,
        },
      },
      {
        path: "refund",
        component: () => import("@/views/order/refund.vue"),
        name: "OrderRefund",
        perm: "order:refund:disabled",
        meta: {
          title: "退款订单",
          hidden: false,
          alwaysShow: false,
        },
      }
    ],
  },
  {
    path: "/goods",
    component: Layout,
    name: "/goods",
    perm: "goods",
    meta: {
      title: "商品管理",
      icon: "el-icon-goods",
      hidden: false,
      alwaysShow: false,
    },
    children: [
      {
        path: "list",
        component: () => import("@/views/goods/list.vue"),
        name: "GoodsList",
        perm: "goods:list",
        meta: {
          title: "商品列表",
          hidden: false,
          alwaysShow: false,
        },
      },
      {
        path: "category",
        component: () => import("@/views/goods/category.vue"),
        name: "GoodsCategory",
        perm: "goods:category",
        meta: {
          title: "商品类别",
          hidden: false,
          alwaysShow: false,
        },
      }
    ],
  },
  {
    path: "/withdraw",
    name: "withdraw",
    component: Layout,
    redirect: "/withdraw/list",
    children: [
      {
        path: "list",
        component: () => import("@/views/withdraw/list.vue"),
        // 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
        // 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        name: "Withdraw",
        meta: {
          title: "提现审核",
          icon: "el-icon-Wallet",
        },
      }
    ],
  },
  {
    path: "/marketing",
    name: "marketing",
    component: Layout,
    redirect: "/marketing/coupon",
    children: [
      {
        path: "coupon",
        component: () => import("@/views/marketing/coupon.vue"),
        name: "coupon",
        meta: {
          title: "优惠券",
          icon: "el-icon-Tickets"
        },
      }
    ],
  },
  {
    path: "/sharing",
    component: Layout,
    name: "/sharing",
    perm: "sharing",
    meta: {
      title: "分润管理",
      icon: "el-icon-Wallet",
      hidden: false,
      alwaysShow: false,
    },
    children: [
      {
        path: "list",
        component: () => import("@/views/sharing/list.vue"),
        name: "SharingList",
        perm: "sharing:list",
        meta: {
          title: "分润记录",
          hidden: false,
          alwaysShow: false,
        },
      },
      {
        path: "withdraw",
        component: () => import("@/views/sharing/withdraw.vue"),
        name: "SharingWithdraw",
        perm: "sharing:withdraw",
        meta: {
          title: "提现记录",
          hidden: false,
          alwaysShow: false,
        },
      }
    ],
  },
  {
    path: "/message",
    component: Layout,
    name: "/message",
    perm: "message",
    meta: {
      title: "消息通知",
      icon: "el-icon-Wallet",
      hidden: false,
      alwaysShow: false,
    },
    children: [
      {
        path: "warning",
        component: () => import("@/views/message/warning.vue"),
        name: "MessageWarning",
        perm: "message:warning",
        meta: {
          title: "临期告警",
          hidden: false,
          alwaysShow: false,
        },
      }
    ],
  },
  {
    path: "/ad",
    name: "ad",
    component: Layout,
    meta: {
      title: "广告管理",
      icon: "el-icon-Money",
      hidden: false,
      alwaysShow: false,
    },
    redirect: "/ad/swiper",
    children: [
      {
        path: "swiper",
        component: () => import("@/views/ad/swiper.vue"),
        name: "swiper",
        meta: {
          title: "首页轮播图",
        },
      },
      {
        path: "standby",
        component: () => import("@/views/ad/standby.vue"),
        name: "standby",
        meta: {
          title: "待机广告",
        },
      }
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app: App<Element>) {
  app.use(router);
}

/**
 * 重置路由
 */
export function resetRouter() {
  router.replace({ path: "/login" });
}

export default router;
